﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace SchEdu.Framework.DataModel
{
    public class WorkCenterKey : IComparable
    {
        SchEduDataSet.WorkCenterRow workCenter = null;

        public SchEduDataSet.WorkCenterRow WorkCenter
        {
            get { return workCenter; }
        }

        public int WorkCenterID
        {
            get { return workCenter.ID; }
        }

        public WorkCenterKey(SchEduDataSet.WorkCenterRow workCenter)
        {
            this.workCenter = workCenter;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            MachineKey key = obj as MachineKey;
            if (key == null)
            {
                throw new ArgumentException("Object is not MachineKey");
            }
            if (this.workCenter.ID < key.WorkCenterID)
            {
                return -1;
            }
            if (this.workCenter.ID > key.WorkCenterID)
            {
                return 1;
            }
            return 0;
        }

        #endregion
    }

    public class WorkCenterOperationList : Dictionary<WorkCenterKey, OperationList>
    {
        public int GetCmax()
        {
            int Cmax = Int32.MinValue;
            // Calculate longest path
            foreach (KeyValuePair<WorkCenterKey, OperationList> a in this)
            {
                Cmax = Math.Max(a.Value.GetProcessingTime(), Cmax);
            }
            return Cmax;
        }

        public OperationList GetNextOperations()
        {
            OperationList op = new OperationList();
            foreach (KeyValuePair<WorkCenterKey, OperationList> item in this)
            {
                if (item.Value.Count == 0)
                {
                    continue;
                }
                op.Add(item.Value[0]);
            }
            return op;
        }

        public bool ContainsWorkCenter(SchEduDataSet.WorkCenterRow workCenter)
        {
            foreach (KeyValuePair<WorkCenterKey, OperationList> a in this)
            {
                if (a.Key.WorkCenterID == workCenter.ID)
                {
                    return true;
                }
            }
            return false;
        }

        public void RemoveByOperation(SchEduDataSet.OperationRow operation)
        {
            foreach (KeyValuePair<WorkCenterKey, OperationList> a in this)
            {
                if (a.Value.Contains(operation) == true)
                {
                    a.Value.Remove(operation);
                }
            }
        }
    }
}
